package com.tuda.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.tuda.common.annotation.Excel;
import com.tuda.common.utils.DateUtils;
import com.tuda.common.utils.ToolUtil;
import com.tuda.system.domain.TudaAppletUser;
import com.tuda.system.mapper.TudaAppletUserMapper;
import com.tuda.system.model.result.OrderRecordResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tuda.system.mapper.TudaOrderRecordMapper;
import com.tuda.system.domain.TudaOrderRecord;
import com.tuda.system.service.ITudaOrderRecordService;
import com.tuda.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 运单操作记录Service业务层处理
 * 
 * @author tuda
 * @date 2023-10-12
 */
@Service
public class TudaOrderRecordServiceImpl implements ITudaOrderRecordService 
{
    @Resource
    private TudaOrderRecordMapper tudaOrderRecordMapper;
    @Resource
    private TudaAppletUserMapper appletUserMapper;

    @Override
    public List<OrderRecordResult> selectOrderRecordList(OrderRecordResult recordResult) {
        List<OrderRecordResult> list = tudaOrderRecordMapper.selectOrderRecordList(recordResult);
        for (OrderRecordResult result : list) {
            TudaOrderRecord t = tudaOrderRecordMapper.querySum();
            if(ToolUtil.isNotEmpty(t)){
                result.setSumIncomeAmount(t.getSumIncomeAmount());
                result.setSumExpenditureAmount(t.getSumExpenditureAmount());
            }
            TudaAppletUser appletUser= appletUserMapper.selectTudaAppletUserById(result.getUserId());
            if(ToolUtil.isNotEmpty(appletUser)){
                result.setCreateUser(appletUser.getNickName());
            }
        }
        return list;
    }

    @Override
    public int insertOrderRecord(Long orderId, String incomeDetails, BigDecimal incomeAmount, String expenditureDetails, BigDecimal expenditureAmount)
    {
        TudaOrderRecord record = new TudaOrderRecord();
        record.setOrderId(orderId);
        record.setIncomeDetails(incomeDetails);
        record.setIncomeAmount(incomeAmount);
        record.setExpenditureDetails(expenditureDetails);
        record.setExpenditureAmount(expenditureAmount);
        record.setCreateTime(DateUtils.getNowDate());
        return tudaOrderRecordMapper.insertTudaOrderRecord(record);
    }

    /**
     * 查询运单操作记录
     * 
     * @param id 运单操作记录主键
     * @return 运单操作记录
     */
    @Override
    public TudaOrderRecord selectTudaOrderRecordById(Long id)
    {
        return tudaOrderRecordMapper.selectTudaOrderRecordById(id);
    }

    /**
     * 查询运单操作记录列表
     * 
     * @param tudaOrderRecord 运单操作记录
     * @return 运单操作记录
     */
    @Override
    public List<TudaOrderRecord> selectTudaOrderRecordList(TudaOrderRecord tudaOrderRecord)
    {
        List<TudaOrderRecord> orderRecords = tudaOrderRecordMapper.selectTudaOrderRecordList(tudaOrderRecord);
        return orderRecords;
    }

    /**
     * 新增运单操作记录
     * 
     * @param tudaOrderRecord 运单操作记录
     * @return 结果
     */
    @Override
    public int insertTudaOrderRecord(TudaOrderRecord tudaOrderRecord)
    {
        tudaOrderRecord.setCreateTime(DateUtils.getNowDate());
        return tudaOrderRecordMapper.insertTudaOrderRecord(tudaOrderRecord);
    }

    /**
     * 修改运单操作记录
     * 
     * @param tudaOrderRecord 运单操作记录
     * @return 结果
     */
    @Override
    public int updateTudaOrderRecord(TudaOrderRecord tudaOrderRecord)
    {
        return tudaOrderRecordMapper.updateTudaOrderRecord(tudaOrderRecord);
    }

    /**
     * 批量删除运单操作记录
     * 
     * @param ids 需要删除的运单操作记录主键
     * @return 结果
     */
    @Override
    public int deleteTudaOrderRecordByIds(String ids)
    {
        return tudaOrderRecordMapper.deleteTudaOrderRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除运单操作记录信息
     * 
     * @param id 运单操作记录主键
     * @return 结果
     */
    @Override
    public int deleteTudaOrderRecordById(Long id)
    {
        return tudaOrderRecordMapper.deleteTudaOrderRecordById(id);
    }
}
